package com.example.demo.controller;

import com.example.demo.controller.exception.BizException;
import com.example.demo.controller.req.OperationStatInsertReq;
import com.example.demo.controller.resp.Response;
import com.example.demo.service.OperationService;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/operation")
public class OperationController {

    @Resource
    OperationService operationService;

    /**
     * 新增内容
     */
    @PostMapping(value = "/insert", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<?> insert(@RequestBody OperationStatInsertReq req) {
        req.validate();
        operationService.insert(req);
        return Response.success("统计成功");
    }

    /**
     * 历史内容
     */
    @GetMapping(value = "/history")
    public Response<?> history(@RequestParam String activeCode) {
        if (StringUtils.isEmpty(activeCode)) {
            throw new BizException("激活码字段缺失");
        }
        return Response.success(operationService.showAll(activeCode));
    }

    /**
     * 统计内容
     */
    @GetMapping(value = "/count")
    public Response<?> count(@RequestParam String activeCode) {
        if (StringUtils.isEmpty(activeCode)) {
            throw new BizException("激活码字段缺失");
        }
        return Response.success(operationService.count(activeCode));
    }
}
